﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FishingSearch
{
    /// <summary>
    /// Interaction logic for OptionsRod.xaml
    /// </summary>
    public partial class OptionsRod : Window
    {

        #region FIELD
        public delegate void OptionsRodEventHandler(object sender, OptionsRodEventArgs opt);
        public event OptionsRodEventHandler OptionsRodUpdated;
        #endregion

        public OptionsRod(BassRodLimit bassRodLimit, BluefishRodLimit bluefishRodLimit, BoatRodLimit boatRodLimit)
        {
            InitializeComponent();

            //*********
            //BASS ROD
            //*********
            //CASTING
            this.txtMinCasting_BassRod.Text = bassRodLimit.MinCasting.ToString();
            this.txtMaxCasting_BassRod.Text = bassRodLimit.MaxCasting.ToString();
            //LENGTH
            this.txtMinLength_BassRod.Text = bassRodLimit.MinLength.ToString();
            this.txtMaxLength_BassRod.Text = bassRodLimit.MaxLength.ToString();
            //ACTION
            this.cbxActionFast_BassRod.IsChecked = bassRodLimit.ActionFast;
            this.cbxActionEstraFast_BassRod.IsChecked = bassRodLimit.ActionExtraFast;
            //POWER
            this.cbxPowerMedium_BassRod.IsChecked = bassRodLimit.PowerMedium;
            this.cbxPowerMedHeavy_BassRod.IsChecked = bassRodLimit.PowerMedHeavy;
            //BRAND
            this.txtBrand_BassRod.Text = bassRodLimit.Brand;

            //*************
            //BLUEFISH ROD
            //*************
            //CASTING
            this.txtMinCasting_BluefishRod.Text = bluefishRodLimit.MinCasting.ToString();
            this.txtMaxCasting_BluefishRod.Text = bluefishRodLimit.MaxCasting.ToString();
            //LENGTH
            this.txtMinLength_BluefishRod.Text = bluefishRodLimit.MinLength.ToString();
            this.txtMaxLength_BluefishRod.Text = bluefishRodLimit.MaxLength.ToString();
            //ACTION
            this.cbxActionFast_BluefishRod.IsChecked = bluefishRodLimit.ActionFast;
            this.cbxActionModFast_BluefishRod.IsChecked = bluefishRodLimit.ActionModFast;
            this.cbxActionModerate_BluefishRod.IsChecked = bluefishRodLimit.ActionModerate;
            //POWER
            this.cbxPowerHeavy_BluefishRod.IsChecked = bluefishRodLimit.PowerHeavy;
            this.cbxPowerModHeavy_BluefishRod.IsChecked = bluefishRodLimit.PowerModHeavy;
            this.cbxPowerExtraHeavy_BluefishRod.IsChecked = bluefishRodLimit.PowerExtraHeavy;
            //BRAND
            this.txtBrand_BlueFish.Text = bluefishRodLimit.Brand;

            //*********
            //BOAT ROD
            //*********
            //CASTING
            this.txtMinCasting_BoatRod.Text = boatRodLimit.MinCasting.ToString();
            this.txtMaxCasting_BoatRod.Text = boatRodLimit.MaxCasting.ToString();
            //LENGTH
            this.txtMinLength_BoatRod.Text = boatRodLimit.MinLength.ToString();
            this.txtMaxLength_BoatRod.Text = boatRodLimit.MaxLength.ToString();
            //ACTION
            this.cbxActionModerate_BoatRod.IsChecked = boatRodLimit.ActionModerate;
            //POWER
            this.cbxPowerHeavy_BoatRod.IsChecked = boatRodLimit.PowerHeavy;
            this.cbxPowerExtraHeavy_BoatRod.IsChecked = boatRodLimit.PowerExtraHeavy;
            //BRAND
            this.txtBrand_BoatRod.Text = boatRodLimit.Brand;

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string rodItem;
            RodLimit rodLimit;
            BassRodLimit bassRodLimit = new BassRodLimit();
            BluefishRodLimit bluefishRodLimit = new BluefishRodLimit();
            BoatRodLimit boatRodLimit = new BoatRodLimit();

            //-------------------------
            // BASS ROD
            //-------------------------
            rodItem = "CANNA DA SPIGOLA DALLA COSTA";
            if (!CheckCastingValue(rodItem, this.txtMinCasting_BassRod, this.txtMaxCasting_BassRod, rodLimit.MinCasting_BassRod, rodLimit.MaxCasting_BassRod))
                return;
            if (!CheckLengthValue(rodItem, this.txtMinLength_BassRod, this.txtMaxLength_BassRod, rodLimit.MinLength_BassRod, rodLimit.MaxLength_BassRod))
                return;

            bassRodLimit.MinCasting = int.Parse(this.txtMinCasting_BassRod.Text);
            bassRodLimit.MaxCasting = int.Parse(this.txtMaxCasting_BassRod.Text);
            bassRodLimit.MinLength = int.Parse(this.txtMinLength_BassRod.Text);
            bassRodLimit.MaxLength = int.Parse(this.txtMaxLength_BassRod.Text);
            bassRodLimit.ActionFast = this.cbxActionFast_BassRod.IsChecked;
            bassRodLimit.ActionExtraFast = this.cbxActionEstraFast_BassRod.IsChecked;
            bassRodLimit.PowerMedium = this.cbxPowerMedium_BassRod.IsChecked;
            bassRodLimit.PowerMedHeavy = this.cbxPowerMedHeavy_BassRod.IsChecked;
            bassRodLimit.Brand = this.txtBrand_BassRod.Text;

            //-------------------------
            // BUEFISH ROD
            //-------------------------
            rodItem = "CANNA DA SERRA DALLA COSTA";
            if (!CheckCastingValue(rodItem, this.txtMinCasting_BluefishRod, this.txtMaxCasting_BluefishRod, rodLimit.MinCasting_BluefishRod, rodLimit.MaxCasting_BluefishRod))
                return;
            if (!CheckLengthValue(rodItem, this.txtMinLength_BluefishRod, this.txtMaxLength_BluefishRod, rodLimit.MinLength_BluefishRod, rodLimit.MaxLength_BluefishRod))
                return;

            bluefishRodLimit.MinCasting = int.Parse(this.txtMinCasting_BluefishRod.Text);
            bluefishRodLimit.MaxCasting = int.Parse(this.txtMaxCasting_BluefishRod.Text);
            bluefishRodLimit.MinLength = int.Parse(this.txtMinLength_BluefishRod.Text);
            bluefishRodLimit.MaxLength = int.Parse(this.txtMaxLength_BluefishRod.Text);
            bluefishRodLimit.ActionFast = this.cbxActionFast_BluefishRod.IsChecked;
            bluefishRodLimit.ActionModFast = this.cbxActionModFast_BluefishRod.IsChecked;
            bluefishRodLimit.ActionModerate = this.cbxActionModerate_BluefishRod.IsChecked;
            bluefishRodLimit.PowerHeavy = this.cbxPowerHeavy_BluefishRod.IsChecked;
            bluefishRodLimit.PowerModHeavy = this.cbxPowerModHeavy_BluefishRod.IsChecked;
            bluefishRodLimit.PowerExtraHeavy = this.cbxPowerExtraHeavy_BluefishRod.IsChecked;
            bluefishRodLimit.Brand = this.txtBrand_BlueFish.Text;

            //-------------------------
            // BOAT ROD
            //-------------------------
            rodItem = "CANNA DA BARCA";
            if (!CheckCastingValue(rodItem, this.txtMinCasting_BoatRod, this.txtMaxCasting_BoatRod, rodLimit.MinCasting_BoatRod, rodLimit.MaxCasting_BoatRod))
                return;
            if (!CheckLengthValue(rodItem, this.txtMinLength_BoatRod, this.txtMaxLength_BoatRod, rodLimit.MinLength_BoatRod, rodLimit.MaxLength_BoatRod))
                return;

            boatRodLimit.MinCasting = int.Parse(this.txtMinCasting_BoatRod.Text);
            boatRodLimit.MaxCasting = int.Parse(this.txtMaxCasting_BoatRod.Text);
            boatRodLimit.MinLength = int.Parse(this.txtMinLength_BoatRod.Text);
            boatRodLimit.MaxLength = int.Parse(this.txtMaxLength_BoatRod.Text);
            boatRodLimit.ActionModerate = this.cbxActionModerate_BoatRod.IsChecked;
            boatRodLimit.PowerHeavy = this.cbxPowerHeavy_BoatRod.IsChecked;
            boatRodLimit.PowerExtraHeavy = this.cbxPowerExtraHeavy_BoatRod.IsChecked;
            boatRodLimit.Brand = this.txtBrand_BoatRod.Text;

            //OptionsRodEventArgs optEvArg = new OptionsRodEventArgs(minCasting_BassRod, maxCasting_BassRod, minLength_BassRod, maxLength_BassRod, actionFast_BassRod, actionExtraFast_BassRod, powerMedium_BassRod, powerMediumHeavy_BassRod,
            //                                                        minCasting_BluefishRod, maxCasting_BluefishRod, minLength_BluefishRod, maxLength_BluefishRod, actionFast_BluefishRod, actionModFast_BluefishRod, actionModerate_BluefishRod, powerMedium_BluefishRod, powerMedHeavy_BluefishRod, powerExtraHeavy_BluefishRod,
            //                                                        minCasting_BoatRod, minLength_BoatRod, maxLength_BoatRod, actionModerate_BoatRod, powerHeavy_BoatRod, powerExtraHeavy_BoatRod);
            OptionsRodEventArgs optEvArg = new OptionsRodEventArgs(bassRodLimit, bluefishRodLimit, boatRodLimit);
            OptionsRodUpdated(this, optEvArg);

            this.Close();

        }

        private bool CheckLengthValue(string rodItem, TextBox txtMinLength, TextBox txtMaxLength, int minLengthLimit, int maxLengthLimit)
        {
            bool res;
            int minLengthValue;
            int maxLengthValue;

            //MINIMUM LENGTH
            res = int.TryParse(txtMinLength.Text, out minLengthValue);
            if (res == false)
            {
                ShowWarning(rodItem, string.Format("Inserire un valore numerico intero per la lunghezza minima."));
                return false;
            }
            if ((minLengthValue > maxLengthLimit) || (minLengthValue < 0))
            {
                ShowWarning(rodItem, string.Format("Inserire un valore per la lunghezza compreso tra {0} e {1}.", "0", maxLengthLimit));
                return false;
            }

            //MAXIMUM LENGTH
            res = int.TryParse(txtMaxLength.Text, out maxLengthValue);
            if (res == false)
            {
                ShowWarning(rodItem, string.Format("Inserire un valore numerico intero per la lunghezza massima."));
                return false;
            }
            if ((maxLengthValue > maxLengthLimit) || (maxLengthValue < 0))
            {
                ShowWarning(rodItem, string.Format("Inserire un valore per la lunghezza compreso tra {0} e {1}.", "0", maxLengthLimit));
                return false;
            }


            //MINIMUM AND MAXIMUM LENGTH
            if (minLengthValue > maxLengthValue)
            {
                ShowWarning(rodItem, "Inserire un valore di lunghezza massima maggiore del minimo.");
                return false;
            }

            return true;

        }

        private bool CheckCastingValue(string rodItem, TextBox txtMinCasting, TextBox txtMaxCasting, int minCastingLimit, int maxCastingLimit)
        {
            bool res;
            int minCastingValue;
            int maxCastingValue;

            //MINIMUM CASTING
            res = int.TryParse(txtMinCasting.Text, out minCastingValue);
            if (res == false)
            {
                ShowWarning(rodItem, "Inserire un valore numerico intero per il casting minimo.");
                return false;
            }
            if ((minCastingValue > maxCastingLimit) || (minCastingValue < minCastingLimit))
            {
                ShowWarning(rodItem, string.Format("Inserire un valore per il casting minimo compreso tra {0} e {1}.", minCastingLimit, maxCastingLimit));
                return false;
            }

            //MAXIMUM CASTING
            res = int.TryParse(txtMaxCasting.Text, out maxCastingValue);
            if (res == false)
            {
                ShowWarning(rodItem, "Inserire un valore numerico intero per il casting massimo.");
                return false;
            }
            if ((maxCastingValue > maxCastingLimit) || (maxCastingValue < minCastingLimit))
            {
                ShowWarning(rodItem, string.Format("Inserire un valore per il casting massimo compreso tra {0} e {1}.", minCastingLimit, maxCastingLimit));
                return false;
            }

            //MINIMUM AND MAXIMUM CASTING
            if (minCastingValue > maxCastingValue)
            {
                ShowWarning(rodItem, string.Format("Inserire un valore di casting massimo maggiore del casting minimo."));
                return false;
            }

            return true;

        }

        private void btnCancel_CLick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ShowWarning(string item, string message)
        {
            message = item + Environment.NewLine + Environment.NewLine + message;
            MessageInfo miWarning = new MessageInfo(message, this);
            miWarning.ShowDialog();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    public class OptionsRodEventArgs : EventArgs
    {
        BassRodLimit _bassRodLimit = new BassRodLimit();
        BluefishRodLimit _bluefishRodLimit = new BluefishRodLimit();
        BoatRodLimit _boatRodLimit = new BoatRodLimit();

        //public OptionsRodEventArgs(int minCasting_BassRod, int maxCasting_BassRod, int minLength_BassRod, int maxLength_BassRod, bool? actionFast_BassRod, bool? actionExtraFast_BassRod, bool? powerMedium_BassRod, bool? powerMediumHeavy_BassRod, int minCasting_BluefishRod, int maxCasting_BluefishRod, int minLength_BluefishRod, int maxLength_BluefishRod, bool? actionFast_BluefishRod, bool? actionModFast_BluefishRod, bool? actionModerate_BluefishRod, bool? powerMedium_BluefishRod, bool? powerMedHeavy_BluefishRod, bool? powerExtraHeavy_BluefishRod, int minCasting_BoatRod, int minLength_BoatRod, int maxLength_BoatRod, bool? actionModerate_BoatRod, bool? powerHeavy_BoatRod, bool? powerExtraHeavy_BoatRod)
        public OptionsRodEventArgs(BassRodLimit bassRodLimit, BluefishRodLimit bluefishRodLimit, BoatRodLimit boatRodLimit)
        {

            //BASS ROD
            this._bassRodLimit.MinCasting= bassRodLimit.MinCasting;
            this._bassRodLimit.MaxCasting = bassRodLimit.MaxCasting;
            this._bassRodLimit.MinLength= bassRodLimit.MinLength;
            this._bassRodLimit.MaxLength = bassRodLimit.MaxLength;
            this._bassRodLimit.ActionFast = bassRodLimit.ActionFast;
            this._bassRodLimit.ActionExtraFast = bassRodLimit.ActionExtraFast;
            this._bassRodLimit.PowerMedium = bassRodLimit.PowerMedium;
            this._bassRodLimit.PowerMedHeavy = bassRodLimit.PowerMedHeavy;
            this._bassRodLimit.Brand = bassRodLimit.Brand;

            //BLUEFISH ROD
            this._bluefishRodLimit.MinCasting = bluefishRodLimit.MinCasting;
            this._bluefishRodLimit.MaxCasting = bluefishRodLimit.MaxCasting;
            this._bluefishRodLimit.MinLength = bluefishRodLimit.MinLength;
            this._bluefishRodLimit.MaxLength = bluefishRodLimit.MaxLength;
            this._bluefishRodLimit.ActionFast = bluefishRodLimit.ActionFast;
            this._bluefishRodLimit.ActionModFast = bluefishRodLimit.ActionModFast;
            this._bluefishRodLimit.ActionModerate = bluefishRodLimit.ActionModerate;
            this._bluefishRodLimit.PowerHeavy = bluefishRodLimit.PowerHeavy;
            this._bluefishRodLimit.PowerModHeavy = bluefishRodLimit.PowerModHeavy;
            this._bluefishRodLimit.PowerExtraHeavy = bluefishRodLimit.PowerExtraHeavy;
            this._bluefishRodLimit.Brand = bluefishRodLimit.Brand;

            //BOAT ROD
            this._boatRodLimit.MinCasting = boatRodLimit.MinCasting;
            this._boatRodLimit.MaxCasting = boatRodLimit.MaxCasting;
            this._boatRodLimit.MinLength = boatRodLimit.MinLength;
            this._boatRodLimit.MaxLength = boatRodLimit.MaxLength;
            this._boatRodLimit.ActionModerate = boatRodLimit.ActionModerate;
            this._boatRodLimit.PowerHeavy = boatRodLimit.PowerHeavy;
            this._boatRodLimit.PowerExtraHeavy = boatRodLimit.PowerExtraHeavy;
            this._boatRodLimit.Brand = boatRodLimit.Brand;

        }

        //BASS ROD
        public int MinCasting_BassRod
        { get { return _bassRodLimit.MinCasting; } }

        public int MaxCasting_BassRod
        { get { return _bassRodLimit.MaxCasting; } }

        public int MinLength_BassRod
        { get { return _bassRodLimit.MinLength; } }

        public int MaxLength_BassRod
        { get { return _bassRodLimit.MaxLength; } }

        public bool? ActionFast_BassRod
        { get { return _bassRodLimit.ActionFast; } }

        public bool? ActionExtraFast_BassRod
        { get { return _bassRodLimit.ActionExtraFast; } }

        public bool? PowerMedium_BassRod
        { get { return _bassRodLimit.PowerMedium; } }

        public bool? PowerMedHeavy_BassRod
        { get { return _bassRodLimit.PowerMedHeavy; } }
        
        public string Brand_BassRod
        { get { return _bassRodLimit.Brand; } }


        //BLUEFISH ROD
        public int MinCasting_BluefishRod
        { get { return _bluefishRodLimit.MinCasting; } }

        public int MaxCasting_BluefishRod
        { get { return _bluefishRodLimit.MaxCasting; } }

        public int MinLength_BluefishRod
        { get { return _bluefishRodLimit.MinLength; } }

        public int MaxLength_BluefishRod
        { get { return _bluefishRodLimit.MaxLength; } }

        public bool? ActionFast_BluefishRod
        { get { return _bluefishRodLimit.ActionFast; } }

        public bool? ActionModFast_BluefishRod
        { get { return _bluefishRodLimit.ActionModFast; } }

        public bool? ActionModerate_BluefishRod
        { get { return _bluefishRodLimit.ActionModerate; } }

        public bool? PowerHeavy_BluefishRod
        { get { return _bluefishRodLimit.PowerHeavy; } }

        public bool? PowerModHeavy_BluefishRod
        { get { return _bluefishRodLimit.PowerModHeavy; } }

        public bool? PowerExtraHeavy_BluefishRod
        { get { return _bluefishRodLimit.PowerExtraHeavy; } }

        public string Brand_BluefishRod
        { get { return _bluefishRodLimit.Brand; } }


        //BOAT ROD
        public int MinCasting_BoatRod
        { get { return _boatRodLimit.MinCasting; } }

        public int MaxCasting_BoatRod
        { get { return _boatRodLimit.MaxCasting; } }

        public int MinLength_BoatRod
        { get { return _boatRodLimit.MinLength; } }

        public int MaxLength_BoatRod
        { get { return _boatRodLimit.MaxLength; } }

        public bool? ActionModerate_BoatRod
        { get { return _boatRodLimit.ActionModerate; } }

        public bool? PowerHeavy_BoatRod
        { get { return _boatRodLimit.PowerHeavy; } }

        public bool? PowerExtraHeavy_BoatRod
        { get { return _boatRodLimit.PowerExtraHeavy; } }
        
        public string Brand_BoatRod
        { get { return _boatRodLimit.Brand; } }

    }

}
